(function(){
	baidu.editor.plugins['binduibutton'] = function(){
		var editor = this;
		editor.addListener('selectionchange', function (){
            
             var stateMap = ['toolbar-item', 'toolbar-item  toolbar-item-active'];
            
			 var pasteState = editor.queryCommandState('PastePlain');
			 var paseBtn = $('paste');
			 paseBtn.className = stateMap[pasteState];

             //绑定加粗按钮的状态
             var boldState = editor.queryCommandState('Bold');
			 var boldBtn = $('bold');
			 boldBtn.className = stateMap[boldState];

             //绑定斜体按钮状态
             var italicState = editor.queryCommandState('italic');
			 var italicBtn = $('italic');
			 italicBtn.className = stateMap[italicState];

            //绑定引用按钮状态
             var blockquoteState = editor.queryCommandState('blockquote');
			 var blockquoteBtn = $('blockquote');
			 blockquoteBtn.className = stateMap[blockquoteState];

            //绑定有序列表按钮状态
			 var numberlistBtn = $('numberlist');
             var bulletlistBtn = $('bulletlist');
             var listType = editor.queryCommandValue('insertorderedlist');
             var listMap = {
                decimal: numberlistBtn,
                disc: bulletlistBtn
             };
             for( var type in listMap){
                 if(listType == type){
                    listMap[ listType ].className = stateMap[1];
                 }else{
                    listMap[ type ].className = stateMap[0];
                 }
             }

		 });

		$('paste').onclick = function(){
			editor.execCommand('PastePlain');
		}
        $('bold').onclick = function(){
			editor.execCommand('bold');
		}
        $('italic').onclick = function(){
            editor.execCommand('italic');
        }
        $('numberlist').onclick = function(){
            editor.execCommand('insertorderedlist', 'decimal');
        }
        $('bulletlist').onclick = function(){
            editor.execCommand('insertorderedlist', 'disc');
        }
        $('blockquote').onclick = function(){
            editor.execCommand('blockquote', {style: 'border-left: 3px solid #D0E5F2; padding-left: 10px'});
        }
		var sr = 1;
		$('searchreplace').onclick = function(){
			var btnClassNameMap = ['toolbar-item', 'toolbar-item  toolbar-item-active'];
			var srIframeStyleMap = ['none', 'block'];
			var srIframeHTML = ['', '<iframe src="searchreplace.html" frameborder="0"></iframe>'];
			$('searchreplace').className = btnClassNameMap[ sr ];
			$('searchreplaceiframe').style.display = srIframeStyleMap[ sr ];
			$('searchreplaceiframe').innerHTML = srIframeHTML[ sr ];
			sr = sr ^ 1;
		}
		
		$('insert').onclick = function(){
			var html = window.prompt('请输入', '<script src="http://img.baidu.com/js/tangram-base-1.3.9.js"><\/script>');
			if( html ){
				html = html.replace(/</g, '&lt;');
				html = html.replace(/>/g, '&gt;');
				editor.execCommand('inserthtml', '<p>' + html + '</p>');
			}
		}
	}

	function $( id ){
		return document.getElementById( id );
	}
})()
